package com.jlnx.controller.chain;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jlnx.dao.ChainEnterpriseDaoImpl;
import com.jlnx.dao.CoreEnterpriseDaoImpl;
import com.jlnx.dao.EnterpriseDaoImpl;
import com.jlnx.dao.FinanceReqDaoImpl;
import com.jlnx.dao.PayStatusDaoImpl;
import com.jlnx.pojo.ChainEnterprise;
import com.jlnx.pojo.CoreEnterprise;
import com.jlnx.pojo.Enterprise;
import com.jlnx.pojo.FinanceReq;


@WebServlet("/ReturnController")
public class ReturnController extends HttpServlet {
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		//获取请求信息
		response.setContentType("application/json;charset=UTF-8;text/html");
		//设置请求编码模式
    	request.setCharacterEncoding("utf-8");
    	
    	int reqId = Integer.parseInt(request.getParameter("reqId"));
		 
		FinanceReqDaoImpl findao = new FinanceReqDaoImpl();
		ChainEnterpriseDaoImpl chaindao = new ChainEnterpriseDaoImpl();
		CoreEnterpriseDaoImpl coredao = new CoreEnterpriseDaoImpl();
		PayStatusDaoImpl paydao = new PayStatusDaoImpl();
		//TODO  修改剩余额度
		 
	     //获取融资信息，查看企业剩余额度
		 FinanceReq financeReq = findao.selectByReqId(reqId);
		 String entNo = financeReq.getEntNo();
		 //获取依托的核心企业
		 String reply = financeReq.getReply();
		 ChainEnterprise chainEnterprise = chaindao.selectByEntNo(entNo);
		 CoreEnterprise coreEnterprise = coredao.selectByEntNo(reply);
		 
		 //获取融资类型，以确定比对谁的限额和剩余额度
		 String reqTool = financeReq.getReqTool();
		 
		 //获取链条企业剩余额度
		 BigDecimal chainRemainLimit = chainEnterprise.getRemainLimit();
		 //获取核心企业剩余额度
		 BigDecimal coreRemainLimit = coreEnterprise.getRemainLimit();
		 
		 BigDecimal reqAmount = financeReq.getReqAmount();
		 
		 System.out.println("企业：" + entNo + "融资金额" + reqAmount + "链条剩余额度" + chainRemainLimit + "核心剩余额度" + coreRemainLimit);
		 
		 if(reqTool.equals("保理")) {//保理：还两方额度/************************************************/
			 //更新企业剩余额度,剩余+金额
			 chainEnterprise.setRemainLimit(chainRemainLimit.add(reqAmount));
			 coreEnterprise.setRemainLimit(coreRemainLimit.add(reqAmount));
			 if(chaindao.updateChainEntMessage(chainEnterprise) < 0) {
				 System.out.println("还款失败！");
				//TODO 失败处理
				 request.setAttribute("isRequestSuccess", false);
				 request.setAttribute("message", "还款失败!");
				 request.getRequestDispatcher("/ReturnStatusSearchController").forward(request, response);
			 }else if(coredao.updateCoreEntMessage(coreEnterprise) < 0){
				 System.out.println("还款失败！");
					//TODO 失败处理
					 request.setAttribute("isRequestSuccess", false);
					 request.setAttribute("message", "还款失败!");
					 request.getRequestDispatcher("/ReturnStatusSearchController").forward(request, response);
			 }else {
			//发起还款，修改还款状态,删除放款表中的记录
				 paydao.deletePayStatusByReqId(reqId);
				if(findao.changeReqStatusById(reqId,5) < 0) {
					 request.setAttribute("isRequestSuccess", false);
					 request.setAttribute("message", "还款失败!");
					request.getRequestDispatcher("/ReturnStatusSearchController").forward(request, response);
				}else {
					 request.setAttribute("isRequestSuccess", true);
					 request.setAttribute("message", "还款成功!");
					request.getRequestDispatcher("/ReturnStatusSearchController").forward(request, response);
				}
			 }
		 }else if(reqTool.equals("反保理")) {//反保理：还核心额度/************************************************/
			 //更新企业剩余额度,剩余+金额
			 coreEnterprise.setRemainLimit(coreRemainLimit.add(reqAmount));
			 if(coredao.updateCoreEntMessage(coreEnterprise) < 0){
				 System.out.println("还款失败！");
					//TODO 失败处理
					 request.setAttribute("isRequestSuccess", false);
					 request.setAttribute("message", "还款失败!");
					 request.getRequestDispatcher("/ReturnStatusSearchController").forward(request, response);
			 }else {
			//发起还款，修改还款状态,删除放款表中的记录
				 paydao.deletePayStatusByReqId(reqId);
				if(findao.changeReqStatusById(reqId,5) < 0) {
					 request.setAttribute("isRequestSuccess", false);
					 request.setAttribute("message", "还款失败!");
					request.getRequestDispatcher("/ReturnStatusSearchController").forward(request, response);
				}else {
					 request.setAttribute("isRequestSuccess", true);
					 request.setAttribute("message", "还款成功!");
					request.getRequestDispatcher("/ReturnStatusSearchController").forward(request, response);
				}
			 }
		 }else {
			//更新企业剩余额度,剩余+金额
			 chainEnterprise.setRemainLimit(chainRemainLimit.add(reqAmount));
			 if(chaindao.updateChainEntMessage(chainEnterprise) < 0) {
				 System.out.println("还款失败！");
				//TODO 失败处理
				 request.setAttribute("isRequestSuccess", false);
				 request.setAttribute("message", "还款失败!");
				 request.getRequestDispatcher("/ReturnStatusSearchController").forward(request, response);
			 }else {
			//发起还款，修改还款状态,删除放款表中的记录
				 paydao.deletePayStatusByReqId(reqId);
				if(findao.changeReqStatusById(reqId,5)<0) {
					 request.setAttribute("isRequestSuccess", false);
					 request.setAttribute("message", "还款失败!");
					request.getRequestDispatcher("/ReturnStatusSearchController").forward(request, response);
				}else {
					 request.setAttribute("isRequestSuccess", true);
					 request.setAttribute("message", "还款成功!");
					request.getRequestDispatcher("/ReturnStatusSearchController").forward(request, response);
				}
			 }
		 }
		 
			
		 
				
	}

}
